<?php
/**
 * Class Page
 * @author :cxh QQ:1120710008
 * @version: 1.0.0
 * $page = new Pagination(12);
 * $page->render();
 * $page->limit_start
 * $page->limit_end;
 */
class Page{
    public $limit_start;
    public $limit_end;
    protected $page;
    protected $next_page = "下一页";
    protected $prev_page = "上一页";
    protected $first_page = "首页";
    protected $last_page = "尾页";
    protected $page_size;
    protected $total_page;
    protected $page_start;
    protected $page_end;
    protected $page_code_num;                   //需要显示的分页码个数
    protected $after_active_page_num;           //在选中分页后的页码个数
    protected $disabled_class = "disabled";     //禁止点击类
    protected $active_class = "active";         //选中类

    public function __construct($count,$page_size=12,$page_code_num=10)
    {
        if(isset($_GET['page']) && $_GET['page'] > 0){
            $this->page = $_GET['page'];
        }else{
            $this->page = 1;
        }

        $this->page_size = $page_size;
        $this->total_page = ceil($count/$this->page_size);
        $this->last_page .= $this->total_page;
        $this->limit_start = ($this->page - 1)*$this->page_size;
        $this->limit_end = $this->page_size;
        $this->page_code_num = $page_code_num;
        $this->after_active_page_num = ceil($this->page_code_num/2) - 1;
    }

    public function getUrl($page){
        //加上分页url参数page=1,2,3...
        $_GET['page'] = intval($page);
        //获取服务url
        $server_url = $_SERVER['REQUEST_URI'];
        //parse_url解析url返回其组成部分,preg_replace正则替换url后面参数
        $url = parse_url(preg_replace("/\/+/i","/",$server_url));
        //获取url参数id=1&page=2...
        $param = http_build_query($_GET);

        return $url['path'].'?'.$param;
    }

    public function render(){
        if($this->total_page < 2){
            return '';
        }

        $html = '<ul class="pagination">';
        $html .= $this->first_prev_page().$this->page_code().$this->next_last_page().'</ul>';

        return $html;
    }

    //需要显示的页码
    public function page_code(){

        if($this->page <= ceil($this->page_code_num/2) + 1){
            $this->page_start = 1;
            $this->page_end = $this->page_code_num > $this->total_page ? $this->total_page:$this->page_code_num;
        }else{
            $page_end = $this->page + $this->after_active_page_num;
            $this->page_end = $page_end > $this->total_page ? $this->total_page:$page_end;
            $this->page_start = $this->page_end - $this->page_code_num + 1;
        }

        $html = "";
        for ($i = $this->page_start; $i <= $this->page_end; $i++){
            if($i == $this->page){
                $html .= '<li class="active"><a href="javascript:void(0);">'.$i.'</a></li>';
            }else{
                $html .= '<li><a href="'.$this->getUrl($i).'">'.$i.'</a></li>';
            }
        }

        return $html;
    }

    //下一页和最后一页
    public function next_last_page(){
        if($this->page > $this->total_page - 1){
            $html = <<<HTML
                    <li class="{$this->disabled_class}">
                        <a href="#"><span>{$this->next_page}</span></a>
                     </li>
                    <li class="{$this->disabled_class}">
                        <a href="#"><span>{$this->last_page}</span></a>
                    </li>
HTML;
        }else{
            $html = <<<HTML
                    <li>
                        <a href="{$this->getUrl($this->page+1)}"><span>{$this->next_page}</span></a>
                    </li>
                    <li>
                        <a href="{$this->getUrl($this->total_page)}"><span>{$this->last_page}</span></a>
                    </li>
HTML;
        }
        return $html;
    }

    //首页和上一页
    public function first_prev_page(){
        if($this->page > 1){
            $html = <<<HTML
                    <li>
                        <a href="{$this->getUrl(1)}"><span>{$this->first_page}</span></a>
                    </li>
                    <li>
                        <a href="{$this->getUrl($this->page-1)}"><span>{$this->prev_page}</span></a>
                    </li>
HTML;
        }else{
            $html = <<<HTML
                    <li class="{$this->disabled_class}">
                        <a href="#"><span>{$this->first_page}</span></a>
                     </li>
                    <li class="{$this->disabled_class}">
                        <a href="#"><span>{$this->prev_page}</span></a>
                    </li>
HTML;
        }
        return $html;
    }

}

